package com.tuanzi.loan.business.repository.family.impl;

import com.tuanzi.loan.business.repository.family.ClassificationRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.family.ClassificationInfoSearchRequest;
import com.tuanzi.loan.vo.response.family.ClassificationInfoListResponse;
import org.jooq.Condition;
import org.jooq.SelectField;
import org.jooq.SelectQuery;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanMemberClassification.LOAN_MEMBER_CLASSIFICATION;

@Repository
public class ClassificationRepositoryCustomImpl extends JOOQBaseRepositoryPlus implements ClassificationRepositoryCustom {

    @Override
    public Page<ClassificationInfoListResponse> list(ClassificationInfoSearchRequest searchRequest, Pageable pageable) {
        SelectQuery selectQuery = getSelectQuery(searchRequest);

        return search(selectQuery,pageable,record -> {
            ClassificationInfoListResponse response = new ClassificationInfoListResponse();
            response.setId(record.get(0,Long.class));
            response.setClassif_name(record.get(1,String.class));
            response.setStatus(record.get(2,Integer.class));
            response.setSort(record.get(3,Integer.class));
            response.setCreatetime(record.get(4,Date.class));
            response.setUpdatetime(record.get(5,Date.class));
            return response;

        });
    }

    @Override
    public Long getLastId() {
        Long lastId = dslContext.fetchOne("  SELECT \n" +
                "    AUTO_INCREMENT AS next_id\n" +
                "  FROM\n" +
                "    INFORMATION_SCHEMA.TABLES \n" +
                "  WHERE TABLE_NAME = 'loan_member_classification' ").into(Long.class);
        return lastId;
    }

    @Override
    public Integer getBiggestSort() {
        Integer biggestSort = dslContext.fetchOne("SELECT MAX(sort) AS sort FROM loan_member_classification").into(Integer.class);
        if(biggestSort == null){
            return 1;
        }
        return biggestSort + 1;
    }

    public SelectQuery getSelectQuery(ClassificationInfoSearchRequest searchRequest) {
        Condition condition = DSL.trueCondition();
        List<SelectField<?>> selectFields = new ArrayList<>();

        selectFields.add(LOAN_MEMBER_CLASSIFICATION.ID);
        selectFields.add(LOAN_MEMBER_CLASSIFICATION.CLASSIF_NAME);
        selectFields.add(LOAN_MEMBER_CLASSIFICATION.STATUS);
        selectFields.add(LOAN_MEMBER_CLASSIFICATION.SORT);
        selectFields.add(LOAN_MEMBER_CLASSIFICATION.CREATETIME);
        selectFields.add(LOAN_MEMBER_CLASSIFICATION.UPDATETIME);

        if(!StringUtils.isEmpty(searchRequest.getClassif_name())){
            condition = condition.and(LOAN_MEMBER_CLASSIFICATION.CLASSIF_NAME .eq(searchRequest.getClassif_name()));
        }
        if(searchRequest.getStatus() != null){
            condition = condition.and(LOAN_MEMBER_CLASSIFICATION.STATUS.eq(searchRequest.getStatus()));
        }
        if(searchRequest.getSort() != null){
            condition = condition.and(LOAN_MEMBER_CLASSIFICATION.SORT.eq(searchRequest.getSort()));
        }
        return dslContext.select(selectFields)
                .from(LOAN_MEMBER_CLASSIFICATION)
                .where(condition).orderBy(LOAN_MEMBER_CLASSIFICATION.SORT)
                .getQuery();
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_MEMBER_CLASSIFICATION;
    }
}
